-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVL Tree - Implementation & Insertion.cpp
149 lines (133 loc) · 3.98 KB
/
AVL Tree - Implementation & Insertion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
using namespace std;
struct NODE {
int key;
NODE *left;
NODE *right;
};
NODE *createNode(int key){
NODE *node = new NODE;
node->key = key;
return node;
}
int treeHeight(NODE *root) {
if (root == NULL){
return 0;
} else {
int left = treeHeight(root->left);
int right = treeHeight(root->right);
return max(left, right)+1;
}
}
NODE *rotate(NODE *root, string direction, NODE *node, string type){
if(type == "LL" || type == "RR"){
if(type == "LL"){
NODE *temp = node;
node = node->left;
temp->left = node->right;
node->right = temp;
} else if(type == "RR"){
NODE *temp = node;
node = node->right;
temp->right = node->left;
node->left = temp;
}
// Point/Build link to the parent/root node
if(direction == "left"){
root->left = node;
} else if(direction == "right"){
root->right = node;
}
return root;
} else if(type == "LR"){
NODE *temp = node->left->right;
node->left->right = temp->left;
temp->left = node->left;
node->left = temp;
return rotate(root, direction, node, "LL");
} else if(type == "RL"){
NODE *temp = node->right->left;
node->right->left = temp->right;
temp->right = node->right;
node->right = temp;
return rotate(root, direction, node, "RR");
}
return root;
}
NODE *balance(NODE *root, NODE *node){
// Calculate the balance factor
int LH = treeHeight(node->left);
int RH = treeHeight(node->right);
int balanceFactor = LH-RH;
if(!(balanceFactor == -1 || balanceFactor == 0 || balanceFactor == 1)){
// Validate parent/root node direction
string direction;
if(node == root->left){
direction = "left";
} else if(node == root->right){
direction = "right";
}
// Check rotation type
if(node->left && node->left->right){
return rotate(root, direction, node, "LR");
} else if(node->right && node->right->left){
return rotate(root, direction, node, "RL");
} else if(node->left && node->left->left){
return rotate(root, direction, node, "LL");
} else if(node->right && node->right->right){
return rotate(root, direction, node, "RR");
}
}
if(node->left) root = balance(root, node->left);
if(node->right) root = balance(root, node->right);
return root;
}
NODE *insert(NODE *root, int key){
if(!root) return createNode(key);
if(key < root->key){
root->left = insert(root->left, key);
root = balance(root, root->left);
} else if(key > root->key) {
root->right = insert(root->right, key);
root = balance(root, root->right);
}
return root;
}
void inorder(NODE *root){
if(!root) return;
inorder(root->left);
cout << root->key << " => ";
inorder(root->right);
}
void print(NODE *root, string indent = "", bool right = true){
if (root != nullptr) {
cout << indent;
if(right){
cout << (indent.length() == 0 ? "Root╌" : "R╌╌╌╌");
indent += " ";
} else {
cout << "L╌╌╌╌";
indent += "┆ ";
}
cout << "[" << root->key << "]" << endl;
print(root->left, indent, false);
print(root->right, indent, true);
}
}
int main(){
int A[] = {14, 17, 11, 7, 53, 4, 13, 12, 8, 60, 19, 16, 20};
int n = sizeof(A)/sizeof(A[0]);
NODE *root = NULL;
for(int i = 0; i < n; i++){
if(i == 0){
root = insert(root, A[i]);
} else {
insert(root, A[i]);
}
}
cout << "Print the tree ↴ " << endl;
cout << "-----------------" << endl;
print(root);
cout << endl << endl << "Traverse the tree ↴ " << endl << "Inorder: ";
inorder(root);
}